import {
    getCartList,
    getCartCounts,
    changeCartNum,
    cartDel
} from '../../api/order.js';
import {
    getProductHot,
    collectAll
} from '../../api/store.js';
import {
    setFormId
} from '../../api/api.js';

const app = getApp();
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '0',
            'title': '购物车',
            'color': false
        },
        navH: 0,
        cartCount: 0,
        goodsHidden: true,
        footerswitch: true,
        host_product: [],
        cartList: [],
        isAllSelect: false, //全选
        selectValue: [], //选中的数据
        selectCountPrice: 0.00,
        isGoIndex: true,
        mer_id: [],
        where: {
            longitude: '',
            latitude: '',
            district: ''
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            navH: app.globalData.navHeight
        });
        if (app.globalData.token) that.setData({
            iShidden: true
        });
    },
   allowFun:function(){
      this.onLoadFun();
   },

    /**
     * 关闭授权
     * 
     */
    onCloseAuto: function() {
        this.setData({
            iShidden: true
        });
    },

    subDel: function(event) {
        var formId = event.detail.formId,
            that = this,
            selectValue = that.data.selectValue;
        setFormId(formId);
        if (selectValue.length > 0)
            cartDel(selectValue).then(res => {
                that.getCartList();
                that.getCartNum();
            });
        else
            return app.Tips({
                title: '请选择产品'
            });
    },
    getSelectValueProductId: function() {
        var that = this;
        var validList = that.data.cartList.valid;
        var selectValue = that.data.selectValue;
        var productId = [];
        if (selectValue.length > 0) {
            for (var index1 in validList) {
                for (var index in validList[index1].list) {
                    if (that.inArray(validList[index1].list[index].id, selectValue)) {
                        productId.push(validList[index1].list[index].product_id);
                    }
                }
            }
        };
        return productId;
    },
    subCollect: function(event) {
        var formId = event.detail.formId,
            that = this,
            selectValue = that.data.selectValue;
        setFormId(formId);
        if (selectValue.length > 0) {
            var selectValueProductId = that.getSelectValueProductId();
            collectAll(that.getSelectValueProductId().join(',')).then(res => {
                return app.Tips({
                    title: res.msg,
                    icon: 'success'
                });
            }).catch(err => {
                return app.Tips({
                    title: err
                });
            });
        } else {
            return app.Tips({
                title: '请选择产品'
            });
        }
    },
    subOrder: function(event) {
        var formId = event.detail.formId,
            that = this,
            selectValue = that.data.selectValue;
        setFormId(formId);
        if (selectValue.length > 0) {
            var valid = this.data.cartList.valid;
            wx.navigateTo({
                url: '/pages/order_confirm/index?cartId=' + selectValue.join(',')
            });
        } else {
            return app.Tips({
                title: '请选择产品'
            });
        }
    },
    checkboxAllChange: function(event) {
        var value = event.detail.value;
        if (value.length > 0) {
            this.setAllSelectValue(1)
        } else {
            this.setAllSelectValue(0)
        }
    },
    setAllSelectValue: function(status) {
        var that = this;
        var selectValue = [];
        var valid = that.data.cartList.valid;
        if (valid.length > 0) {
            for (var index1 in valid) {
                for (var index in valid[index1].list) {
                    if (status == 1) {
                        valid[index1].list[index].checked = true;
                        selectValue.push(valid[index1].list[index].id);
                    } else valid[index1].list[index].checked = false;
                }
            }
            var validData = "cartList.valid";

            that.setData({
                [validData]: valid,
                selectValue: selectValue,
            });
            if (status == 1) {
                that.switchSelect();
            } else {
                that.setData({
                    selectCountPrice: '0.00'
                });
                that.setData({
                    cartCount: 0
                });
            }

        }
    },
    checkboxChange: function(event) {
        var that = this;
        var value = event.detail.value;
        var valid = this.data.cartList.valid;
        var num1 = 0
        var mer = that.data.mer_id;

        for (var index1 in valid) {
            for (var index in valid[index1].list) {
                if (that.inArray(valid[index1].list[index].id, value)) {
                    valid[index1].list[index].checked = true;
                } else {
                    valid[index1].list[index].checked = false;

                }
                num1++;
            }
        }

        var validData = "cartList.valid";
        this.setData({
            [validData]: valid,
            isAllSelect: value.length == num1,
            selectValue: value,
            mer_id: mer,
        })
        this.switchSelect();
    },
    inArray: function(search, array) {
        for (var i in array) {
            if (array[i] == search) {
                return true;
            }
        }
        return false;
    },
    deleteIndex: function(arr, i) {
        var index = arr.indexOf(i);
        arr.splice(index, 1)
    },
    switchSelect: function(status = null) {
        var that = this;
        var validList = that.data.cartList.valid;
        var selectValue = that.data.selectValue;
        var selectCountPrice = 0.00;
        if (selectValue.length < 1) {
            that.setData({
                selectCountPrice: selectCountPrice
            });
        } else {
            var num1 = 0;
            for (var index1 in validList) {
                for (var index in validList[index1].list) {
                    if (that.inArray(validList[index1].list[index].id, selectValue)) {
                        selectCountPrice = Number(selectCountPrice) + Number(validList[index1].list[index].cart_num) * Number(validList[index1].list[index].truePrice)
                        num1 = Number(num1) + Number(validList[index1].list[index].cart_num);
                    }
                }
            }
            that.setData({
                selectCountPrice: selectCountPrice.toFixed(2)
            });
            that.setData({
                cartCount: num1
            });
        }
    },
    subCart: function(event) {
        var that = this;
        var status = false;
        var index = event.currentTarget.dataset.index;
        var indexs = index.split("-");
        var item = that.data.cartList.valid[indexs[0]].list[indexs[1]];
        item.cart_num = item.cart_num - 1;
        if (item.cart_num < 1) status = true;
        if (item.cart_num <= 1) {
            item.cart_num = 1;
            item.numSub = true;
        } else {
            item.numSub = false;
            item.numAdd = false;
        }
        if (false == status) {
            that.setCartNum(item.id, item.cart_num, function(data) {
                var itemData = "cartList.valid[" + indexs[0] + "].list[" + indexs[1] + "]";
                that.setData({
                    [itemData]: item
                });
                that.switchSelect();
            });
        }
    },
    addCart: function(event) {
        var that = this;
        var index = event.currentTarget.dataset.index;
        var indexs = index.split("-");
        var item = that.data.cartList.valid[indexs[0]].list[indexs[1]];
        item.cart_num = item.cart_num + 1;
        var productInfo = item.productInfo;
        if (productInfo.hasOwnProperty('attrInfo') && item.cart_num >= item.productInfo.attrInfo.stock) {
            item.cart_num = item.productInfo.attrInfo.stock;
            item.numAdd = true;
            item.numSub = false;
        } else if (item.cart_num >= item.productInfo.stock) {
            item.cart_num = item.productInfo.stock;
            item.numAdd = true;
            item.numSub = false;
        } else {
            item.numAdd = false;
            item.numSub = false;
        }
        that.setCartNum(item.id, item.cart_num, function(data) {
            var itemData = "cartList.valid[" + indexs[0] + "].list[" + indexs[1] + "]";
            that.setData({
                [itemData]: item
            });
            that.switchSelect();
        });
    },
    setCartNum(cartId, cartNum, successCallback) {
        var that = this;
        changeCartNum(cartId, cartNum).then(res => {
            app.globalData.cart_count = res.data.cart_count.toString();
            wx.setTabBarBadge({
                index: 3,
                text: app.globalData.cart_count
            })
            that.setData({
                cartCount: res.data.cart_count
            });
            successCallback && successCallback(res.data);
        }).catch(res => {
            app.Tips({
                title: res
            });
        });
    },
    getCartNum: function() {
        var that = this;
        getCartCounts().then(res => {
            that.setData({
                cartCount: res.data.count
            });
            app.globalData.cart_count = res.data.count.toString();
            if (app.globalData.cart_count > 0) {
                wx.setTabBarBadge({
                    index: 3,
                    text: app.globalData.cart_count
                })
            } else {
                wx.removeTabBarBadge({
                    index: 3
                })
            }
        });
    },
    getCartList: function() {
        var that = this;
        getCartList(that.data.where).then(res => {
            var cartList = res.data;
            var valid = cartList.valid;
            var numSub = [{
                numSub: true
            }, {
                numSub: false
            }];
            var numAdd = [{
                    numAdd: true
                }, {
                    numAdd: false
                }],
                selectValue = [];;
            if (valid.length > 0) {
                var num1 = 0
                for (var index1 in valid) {
                    for (var index in valid[index1].list) {
                        if (valid[index1].list[index].cart_num == 1) {
                            valid[index1].list[index].numSub = true;
                        } else {
                            valid[index1].list[index].numSub = false;
                        }
                        var productInfo = valid[index1].list[index].productInfo;
                        if (productInfo.hasOwnProperty('attrInfo') && valid[index1].list[index].cart_num == valid[index1].list[index].productInfo.attrInfo.stock) {
                            valid[index1].list[index].numAdd = true;;
                        } else if (valid[index1].list[index].cart_num == valid[index1].list[index].productInfo.stock) {
                            valid[index1].list[index].numAdd = true;;
                        } else {
                            valid[index1].list[index].numAdd = false;
                        }
                        valid[index1].list[index].checked = true;
                        selectValue.push(valid[index1].list[index].id);
                        num1++;
                    }
                }
            }
            that.setData({
                cartList: cartList,
                goodsHidden: cartList.valid.length <= 0 ? false : true,
                selectValue: selectValue,
                isAllSelect: num1 == selectValue.length && num1 > 0
            });
            that.switchSelect();
        });
    },
    getHostProduct: function() {
        var that = this;
        getProductHot().then(res => {
            that.setData({
                host_product: res.data
            });
        });
    },
    goodsOpen: function() {
        var that = this;
        that.setData({
            goodsHidden: !that.data.goodsHidden
        })
    },
    manage: function() {
        var that = this;
        that.setData({
            footerswitch: !that.data.footerswitch
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    onLoadFun: function() {
        util.getlocation(this);
        this.getHostProduct();
        this.getCartList();
        this.getCartNum();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (app.globalData.isLog == true) {
           util.getlocation(this);
            this.getHostProduct();
            this.getCartList();
            this.getCartNum();
            this.setData({
                goodsHidden: true,
                footerswitch: true,
                host_product: [],
                cartList: [],
                isAllSelect: false, //全选
                selectValue: [], //选中的数据
                selectCountPrice: 0.00,
                cartCount: 0,
                iShidden: true
            });
        }
    },
    unsetCart: function() {
        let that = this,
            ids = [];
        for (var i = 0, len = that.data.cartList.invalid.length; i < len; i++) {
            ids.push(that.data.cartList.invalid[i].id);
        }
        cartDel(ids).then(res => {
            app.Tips({
                title: '清除成功'
            });
            that.setData({
                'cartList.invalid': []
            });
        }).catch(res => {

        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

})